﻿using System;
using System.Linq;
using Hjt212Parser;
using log4net;
using TcpServer.Facade;
using TcpServer.Orm;

namespace TcpServer.DataProcessor
{
    class Platform3081Processor : IProcessor
    {
        private readonly ILog log = LogManager.GetLogger(typeof(Platform3081Processor));

        public void Persist()
        {
            throw new NotImplementedException();
        }

        public void Persist(DustEntities context, SocketClientInfo content)
        {
            if (string.IsNullOrWhiteSpace(content.MN)) return;
            try
            {
                //有效字符串
                if (Parser.IsLengthRight(content.ReceivedMsg))
                {
                    //根据MN号获取基本信息
                    var equipment = BaseInfoBufferAndHelper.GetBaseWholeInfoByMn(content.MN);
                    //有效企业信息
                    if (equipment != null)
                    {
                        var monitor =
                            context.RealDataMonitors.FirstOrDefault(
                                t => t.StatCodeID == equipment.StatCodeID && t.DevCodeID == equipment.DevCodeID);
                        if (monitor == null) return;
                        var power = Parser.GetStringByKey("Power", content.ReceivedMsg);
                        if (string.IsNullOrEmpty(power))
                        {
                            power = "--";
                        }
                        else if (power.ToUpper().Equals("ON") || power.Equals("1"))
                        {
                            power = "市电供电";
                        }
                        else if (power.ToUpper().Equals("OFF") || power.Equals("0"))
                        {
                            power = "UPS供电";
                        }
                        else
                        {
                            power = "--";
                        }
                        monitor.Power = power;
                        context.SaveChanges();
                        var dataTime = Parser.GetDateTimeByKey("DataTime", content.ReceivedMsg);

                        var rc = new PowerRecord()
                        {
                            监测点编号StatCodeID = equipment.StatCodeID,
                            设备编号DevCodeID = equipment.DevCodeID,
                            时间 = dataTime.GetValueOrDefault(),
                            市电状态 = power
                        };
                        context.PowerRecords.Add(rc);
                        context.SaveChanges();
                    }
                    else
                    {
                        log.Warn( content.MN + " 没有对应基础信息！");
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error( ex);
                log.Error( content.ReceivedMsg);

            }
        }
        public void Persist(DataCommunicationEntities context, SocketClientInfo content)
        {
            throw new NotImplementedException();
        }
    }
}
